using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;


public class ClassData 
{
        //protected string Connstr = (string)ConfigurationSettings.AppSettings["ConnSQL1"];

        protected static string Connstr = ConfigurationManager.ConnectionStrings["SqlServices"].ToString();

        // Ham ket noi Data
        private static SqlConnection GetConnectionString()
        {
            SqlConnection myConn = new SqlConnection(Connstr);
            if (myConn.State == ConnectionState.Open)
                myConn.Close();


            myConn.Open();
            return myConn;
        }

        public static string ReturnString(string strSql)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                string stringValue = "";
                SqlCommand myCommand = new SqlCommand(strSql, myConn);
                myCommand.CommandType = CommandType.Text;
                stringValue = Convert.ToString(myCommand.ExecuteScalar());
                //myConn.Close();
                return stringValue;
            }
        }

        public static DateTime ReturnDateTime(string sql)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                DateTime Value = DateTime.Now;
                SqlCommand myCommand = new SqlCommand(sql, myConn);
                myCommand.CommandType = CommandType.Text;
                Value = (DateTime)myCommand.ExecuteScalar();
                myConn.Close();
                return Value;
            }
        }

        public static int ReturnInteger(string strSql)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                int intValue = 0;
                SqlCommand myCommand = new SqlCommand(strSql, myConn);
                myCommand.CommandType = CommandType.Text;
                intValue = (int)myCommand.ExecuteScalar();
                myConn.Close();
                return intValue;
            }
        }

        public static DataSet ReturnDataSet(string strSql,string myTable)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                DataSet myDataset = new DataSet();
                SqlDataAdapter myAdapter = new SqlDataAdapter(strSql, myConn);
                myAdapter.Fill(myDataset, myTable);
                //myConn.Close();
                return myDataset;
            }
        }

        public static void ExecuteSQL(string sql)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                SqlCommand myCommand = new SqlCommand(sql, myConn);
                myCommand.CommandType = CommandType.Text;
                myCommand.ExecuteNonQuery();
                //myConn.Close();
            }
        }

        
}
